<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends MY_Controller {

	private $list_cat;
	public function __construct(){
		parent::__construct();
		$this->list_cat = $this->cm->get_all($this->tbl->cat,array('mid'=>$this->mid),'',0,'a');
	}
	
	function add(){

		if(!empty($this->p['id'])){
			$this->_data['r'] = $this->cm->get_by_id('custom_product',intval($this->p['id']));
		}

		$this->_data['opt_cat'] = getCatOpt($this->list_cat);
		$this->load->view('product/add',$this->_data);
	}
	
	function index(){

		$this->load->library('pagination');
	    $limit_num = !empty($this->p['per_page']) ? intval($this->p['per_page']) : 0;
		$conf_page = config_item('pagefollow');

		$cond = array();
		if(!empty($this->p['no'])){
			$cond['no|l'] = $this->p['no'];
		}

		$this->_data['list'] = $this->cm->get_page('custom_product','*',$cond,
			array('img|s'=>'images:id,path'),
			$limit_num,$conf_page['per_page'],$ord='',$rt = 'o');


	    //分页导航
	    $this->pagination->initialize($conf_page); 
    	$this->pagination->base_url = $this->page_url();
	    $this->pagination->total_rows = $this->cm->count;
	    $this->_data['pagenav'] = $this->pagination->create_links();

		$this->load->view('product/index',$this->_data);
	}

	function save_do(){

		$no = $this->p['no'];

		if(empty($this->p['id'])){

			if($this->cm->in('custom_product',array('no'=>$no))) ejd('fail','编号不能重复！');
		}else{
			if($this->cm->in('custom_product',array('no'=>$no,'id != '=>$this->p['id']))) ejd('fail','编号不能重复！');
		}

		$this->cm->save('custom_product',$this->p);
		ejd('succ-reload','操作成功！');
	}
	
	function del_do(){
		$id = $this->p['id'];

		//有子分类禁止删除操作
		$this->cm->delete('custom_product',array('id'=>$id));
		ejd('succ-reload','操作成功！');

	}
	

}

